package net.tv90.bfmccore.util;

import java.util.*;

/**
 * 单个键值对
 * @param <K> 键
 * @param <V> 值
 */

public class KVPair<K, V> {
    private K Key;
    private V Value;

    public KVPair() {
        this.Key = null;
        this.Value = null;
    }

    public KVPair(K key, V value) {
        this.Key = key;
        this.Value = value;
    }

    /**
     * 将只具有单个键值对的HashMap序列化为键值对
     * @param map 键值对
     */
    public KVPair(HashMap<Object, Object> map) {
        Collection<Object> valueList = Collections.singletonList(map.values());
        this.Key = (K) map.keySet().iterator().next();
        this.Value = (V) valueList.iterator().next();
    }

    public K getKey() {
        return this.Key;
    }

    public V getValue() {
        return this.Value;
    }

    public void setKey(K key) {
        this.Key = key;
    }

    public void setValue(V value) {
        this.Value = value;
    }

    /**
     * 序列化为具有单个键和值的HashMap
     * @return HashMap
     */
    public Map<Object, Object> toHashMap() {
        HashMap<Object, Object> map = new HashMap<>();
        map.put(this.Key, this.Value);
        return map;
    }

    @Override
    public String toString() {
        return "KVPair{" + "Key=" + Key + ", Value=" + Value + '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof KVPair)) return false;
        KVPair<?, ?> pair = (KVPair<?, ?>) o;
        return Objects.equals(Key, pair.Key) && Objects.equals(Value, pair.Value);
    }

    @Override
    public int hashCode() {
        return Objects.hash(Key, Value);
    }
}
